package reflect;

import java.util.Scanner;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入类名");
        String className = scan.nextLine();
        Class cls = Class.forName(className);

        /*
        通过newInstance()方法实例化对象
        假设cls为String对象，那么newInstance()方法就相当于new String()

         */

        Object obj = cls.newInstance();//只能使用无参构造器实例化
        System.out.println(obj);
    }
}
